﻿package fui.FUIComponents{
	import flash.events.*;
	import flash.display.Sprite;
	import fl.controls.*;
	import fl.containers.ScrollPane;
	import fui.FUIControllers.*;
	import fui.FUIData.IDBClient;
	import fui.FUIData.DBClientV2;
	//Class profile should support registration action and profile editing.
	// Splited on 3 parts : First part : Personal details 
	//						Second part: Password Setup
	//						Third part : Location(Address) setup
	
	public class Profile extends Sprite implements IComponent{
		private var _comName:String;
		private var _type:int = 0; // 0 - view, 1 - registration, 2 - edit
		private var _uis:UISession;
		private var _title:Label;
		private var _fields_names:Array = new Array("Nick Name","First Name", "Last Name",
     												"Gender", "Age", "Language", "E-Mail");
		
		private var _fields_names_part2:Array = new Array("Country", "City", "Street",
														  "zip code", "phone", "alternative phone");
		private var _fLabels:Array;
		private var _fText:Array;
		private var _User:Object;
		private var _fstPanel:ScrollPane;
		private var _sndPanel:ScrollPane;
		private var _passwordsPanel:ScrollPane;
		
		//
		private var nickNameTxt:TextInput;
		private var firstNameTxt:TextInput;
		private var lastNameTxt:TextInput;
		private var emailTxt:TextInput;
		private var ageTxt:TextInput;
		private var genderCmBox:ComboBox;
		private var langCmBox:ComboBox;
		private var passwdTxt:TextInput;
		private var confpasswdTxt:TextInput;
		///
		private var _addrArray:Array;
		///

		private var _nxtBtn:Button;
		
		private var _subbutton:Button;
		private var _cnclbutton:Button;
		private var _DBCon:IDBClient;
		
		private var _ifStarted:Boolean = false;
		private var _step:int = 1;
		
		
		public function Profile(type:int, dbCon:IDBClient)
		{
			this._type = type;
			trace("Profile Class inited. type:" + type);
			this._DBCon = dbCon;
			_User = new Object();// -- will contain collected registration data
			_User.completed = false;
			trace("Profile Class inited. type:" + type);
			if(type == 1)
			{
				this._comName = "Registration";
				createView();
			}
			else if(type == 2)
			{
				this._comName = "Edit Profile";
			}
			
			
		}
		public function showEditProfile():void
		{
			createView();
		}
		private function createView():void{
			_title = new Label();
			_title.text = "Registration Form";
			_title.x = 60;
			_title.y = 20;
			_title.setSize(200, 20);
			addChild(_title);
			_nxtBtn = new Button();
			_nxtBtn.label = "Next";
			_nxtBtn.x = 270;
			_nxtBtn.y = 400;
			_nxtBtn.addEventListener(MouseEvent.CLICK, onNext);
			addChild(_nxtBtn);
			_cnclbutton = new Button();
			_cnclbutton.x = 100;
			_cnclbutton.y = 400;
			_cnclbutton.label = "Cancel";
			_cnclbutton.addEventListener(MouseEvent.CLICK, onCancel);
			addChild(_cnclbutton);
			

			create1stPanel();
			//generateFields();
			
		}
		
		private function create1stPanel():void{
			// First Registration Panel will contain:
			// Nick Name, Name, Last Name, email, Age, Gender, Language
			trace("[Profile]: start create1stPanel");
			if (_ifStarted == false)
			{
				_ifStarted = true;
				_fstPanel = new ScrollPane();
				_fstPanel.x = 50;
				_fstPanel.y = 40;
				_fstPanel.setSize(440, 350);
			trace("Panel created");
				var _fstLbls:Array = new Array();
				for (var i=0; i <_fields_names.length; i++)
				{
					_fstLbls[i] = new Label();
					_fstLbls[i].text = _fields_names[i];
					_fstLbls[i].x = 100;
					_fstLbls[i].y = i*40 +40;
					_fstPanel.addChild(_fstLbls[i]);
				}
			// data containers:)
			
				nickNameTxt = new TextInput();
				firstNameTxt = new TextInput();
				lastNameTxt = new TextInput();
				emailTxt = new TextInput();
				ageTxt = new TextInput();
				genderCmBox =  new ComboBox();
				langCmBox =  new ComboBox();
			
				nickNameTxt.x = 100+150;
				nickNameTxt.y = 0 * 40 + 40;
				nickNameTxt.width = 150;
				firstNameTxt.x = 100+150;
				firstNameTxt.y = 1 * 40 + 40;
				firstNameTxt.width = 150;
				lastNameTxt.x = 100+150;
				lastNameTxt.y = 2 * 40 + 40;
				lastNameTxt.width = 150;
				ageTxt.x = 100+150;
				ageTxt.y = 4*40 + 40;
				ageTxt.width = 50;
			
				emailTxt.x = 100+150;
				emailTxt.y = 6 * 40 + 40;
				emailTxt.width = 150;
			
				genderCmBox.x = 100+150;
				genderCmBox.y = 3 * 40 + 40;
				genderCmBox.addItem({label:"Male", data:1});
				genderCmBox.addItem({label:"Female", data:2});
				langCmBox.x = 100+150;
				langCmBox.y = 5 * 40 + 40;
				langCmBox.addItem({label:"English", data:1});
				_fstPanel.addChild(nickNameTxt);
				_fstPanel.addChild(firstNameTxt);
				_fstPanel.addChild(lastNameTxt);
				_fstPanel.addChild(emailTxt);
				_fstPanel.addChild(ageTxt);
				_fstPanel.addChild(genderCmBox);
				_fstPanel.addChild(langCmBox);
				
				if(this._type == 2)
				{
					
					nickNameTxt.text = _uis.profile.name;
					firstNameTxt.text = _uis.profile.f_name;
					lastNameTxt.text = _uis.profile.l_name;
					ageTxt.text = _uis.profile.age;
					emailTxt.text = _uis.profile.email;
				}
			//buttons
			
			
				addChild(_fstPanel);
			}
			else
			{
				_sndPanel.visible = false;
				_fstPanel.visible = true;
				_cnclbutton.label = "Cancel";
				_cnclbutton.removeEventListener(MouseEvent.CLICK, onPrev);
				_cnclbutton.addEventListener(MouseEvent.CLICK, onCancel);
			}
			
		}
		private function requestPWD():void{
			_fstPanel.visible = false;
			_passwordsPanel = new ScrollPane();
			_passwordsPanel.x = 50;
			_passwordsPanel.y = 40;
			_passwordsPanel.setSize(440, 350);
			
			var pwd:Label = new Label();
			pwd.x = 100;
			pwd.y = 40 + 40;
			pwd.setSize(120, 30);
			pwd.text = "Enter password:";
			_passwordsPanel.addChild(pwd);
			var pwdconf:Label = new Label();
			pwdconf.x = 100;
			pwdconf.y = 40 + 80;
			pwdconf.setSize(120, 30);
			pwdconf.text = "Confirm password:";
			_passwordsPanel.addChild(pwdconf);
			passwdTxt = new TextInput();
			confpasswdTxt = new TextInput();
			
			passwdTxt.x = 100 + 150;
			passwdTxt.y = 40 + 40;
			passwdTxt.displayAsPassword = true;
			_passwordsPanel.addChild(passwdTxt);
			confpasswdTxt.x = 100 +150;
			confpasswdTxt.y = 40 + 80;
			confpasswdTxt.displayAsPassword = true;
			_passwordsPanel.addChild(confpasswdTxt);
			_cnclbutton.label = "Prev";
			_cnclbutton.removeEventListener(MouseEvent.CLICK, onCancel);
			_cnclbutton.addEventListener(MouseEvent.CLICK, onPrev);
			
			
			addChild(_passwordsPanel);
		}
		private function create2ndPanel():void{
			// Second Registration Panel will contain:
			// 
			_passwordsPanel.visible = false;
			_sndPanel = new ScrollPane();
			_sndPanel.x = 50;
			_sndPanel.y = 40;
			_sndPanel.setSize(440, 350);
			var _sndLbls:Array = new Array();
			_addrArray = new Array();
				for (var i=0; i <_fields_names_part2.length; i++)
				{
					_sndLbls[i] = new Label();
					_sndLbls[i].text = _fields_names_part2[i];
					_sndLbls[i].x = 100;
					_sndLbls[i].y = i*40 +40;
					_sndPanel.addChild(_sndLbls[i]);
					_addrArray[i] = new TextInput();
					_addrArray[i].x = 100 + 150;
					_addrArray[i].y = i*40 + 40;
					if(this._type == 2)
					{
						//_addrArray[i].text = _uis.profile[i+5];
					}
					_sndPanel.addChild(_addrArray[i]);
					
				}
				
			addChild(_sndPanel);
			_nxtBtn.label = "Submit";
			_nxtBtn.removeEventListener(MouseEvent.CLICK, onNext);
			_nxtBtn.addEventListener(MouseEvent.CLICK, onSubmit);
		}
		private function onNext(ev:Event):void
		{
			trace("Step: " + _step);
			if(this._step == 1)
			{
				this._User.name = nickNameTxt.text;
				this._User.fname = firstNameTxt.text;
				this._User.lname = lastNameTxt.text;
				this._User.email = emailTxt.text;
				this._User.age = ageTxt.text;
				this._User.lang = langCmBox.selectedItem.data;
				if(genderCmBox.selectedItem.data == 1)
				{
					this._User.gender = "Male";
				}
				else 
				{
					this._User.gender = "Female";
				}
				this._step = 2;
				requestPWD();

			}
			else if (this._step == 2)
			{
				_passwordsPanel.visible = false;
				this._User.password = passwdTxt.text;
				create2ndPanel();
				this._step = 3;
			}
			else
			{
				trace("step 3");
				
				
			}
		}
		private function onPrev(ev:Event):void
		{
			if (this._step == 3)
			{
				_nxtBtn.label = "Next";
				_nxtBtn.removeEventListener(MouseEvent.CLICK, onSubmit);
				_nxtBtn.addEventListener(MouseEvent.CLICK, onNext);
				_sndPanel.visible = false;
				_passwordsPanel.visible = true;

				this._step = 2;
			}
			else if(this._step == 2)
			{
				_passwordsPanel.visible = false;
				this.create1stPanel();
				this._step = 1;
				
			}
			else
			{
				
				trace("step 3");
			}
		
		}
		
		public function onSubmit(ev:Event):void
		{
			this._User.country = _addrArray[0].text;
			this._User.city    = _addrArray[1].text;
			this._User.street  = _addrArray[2].text;
			this._User.zipcode = _addrArray[3].text;
			this._User.phone   = _addrArray[4].text;
			this._User.phone2  = _addrArray[5].text;
			_User.completed = true;
			
			this._DBCon.addEventListener(Event.COMPLETE, checkResult);
			if (_type ==1)
			{
				this._DBCon.CallStoredProcedure("add_new_player_wrapper",_User);
			}
			else if (_type == 2)
			{
				this._DBCon.CallStoredProcedure("update_player_wrapper",_User);
			}
		}
		private function checkResult(ev:Event):void
		{
			trace("---------------------------");
			trace("DB update finished");
			trace(_DBCon.CallResponse.rc);
			if (_DBCon.CallResponse.rc == true)
			{
				//Print out Registration succesfuly completed
				for (var i:int = 0; i < numChildren; i++) {
    				removeChild(getChildAt(i));
    				i--; // because now the child list has decreased by one, aka the clip you just removed 
				}
				_title = new Label();
			
				_title.x = 60;
				_title.y = 20;
				_title.setSize(200, 20);
				_title.text = "Registration succesfuly completed";
				addChild(_title);
				_cnclbutton = new Button();
				_cnclbutton.x = 100;
				_cnclbutton.y = 400;
				_cnclbutton.label = "Close";
				_cnclbutton.addEventListener(MouseEvent.CLICK, onExit);
				addChild(_cnclbutton);
			}
			else
			{
				
				//print out: Registration failed
			}
		}
		public function onExit(ev:Event):void
		{
			removeChild(_title);
			removeChild(_cnclbutton);
			_step = 1;
			_ifStarted = false;
			createView();
			hide_component();
		}
		
		public function onCancel(ev:Event):void
		{
			hide_component();
		}
		public function set_xml_source(xml_path:String):void
		{
			//TODO:
		}
		public function set_session(uis:UISession):void
		{
			this._uis = uis;
		}
		public function get_component_title():String
		{
			return this._comName;
		}
        public function show_component():void
		{
			//TODO:
			if( _type == 2)
			{
				showEditProfile();
			}
			visible = true;
			trace("[Profile] : visible = true");
		}
		public function hide_component():void
		{
			visible = false;
			trace("[Profile] : visible = false");
		}
		public function if_need_auth():Boolean
		{
			//TODO:
			if(this._type == 2)
				return true;
			else
				return false;
		}
		public function self_remove():void
		{
			//TODO:
		}
	}
}